<?php

use App\Models\FrontendUser;
use Illuminate\Support\Facades\Auth;

require __DIR__ . '/../vendor/autoload.php';

$app = require_once __DIR__ . '/../bootstrap/app.php';

$app->make(\App\Console\Kernel::class)->bootstrap();

/* ============== Write Codes Below ================= */

echo '>>> Start Laravel from CLI' . PHP_EOL;

/** @var \App\Services\WalletService $walletService */
$walletService = app()->get(\App\Services\WalletService::class);

$loop = 100;
$agentId = 3;

for ($i = 0; $i < $loop; ++$i) {
    $mobile = 18900000000 + $i;
    $user = FrontendUser::query()
        ->where('mobile', $mobile)
        ->where('agent_id', $agentId)
        ->first();
    if (!$user) {
        $user = new FrontendUser();
        $user->mobile = $mobile;
        $user->agent_id = $agentId;
        $user->password = \Hash::make('111111');
        $user->wallet_address = $walletService->openWallet($mobile);
        $user->save();
    }
    if ('' === $user->wallet_address) {
        $user->wallet_address = $walletService->openWallet($mobile);
        $user->save();
    }
    $token = Auth::guard(\App\Constant::GUARD_API)->login($user);
    echo $user->mobile . ',' . $token . PHP_EOL;
}
